package com.flyme.module.base.provider.controller;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.flyme.common.core.model.ResultBody;
import com.flyme.common.core.model.query.CriteriaQuery;
import com.flyme.common.core.model.query.PagerInfo;
import com.flyme.common.core.model.query.PqGrid;
import com.flyme.core.base.supper.controller.BaseController;
import com.flyme.core.base.supper.entity.EntityMap;
import com.flyme.module.base.client.api.SysFeedBackRemoteService;
import com.flyme.module.base.client.dto.SysFeedbackDto;
import com.flyme.module.base.client.entity.SysFeedback;
import com.flyme.module.base.client.entity.SystemUserRole;
import com.flyme.module.base.provider.service.SysFeedbackService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 留言管理
 *
 * @author zyf
 */
@Api(tags = "留言管理")
@RestController
public class SysFeedBackController extends BaseController<SysFeedbackService, SysFeedback> implements SysFeedBackRemoteService {


    @ApiOperation(value = "列表", notes = "列表")
    @Override
    public ResultBody list(@ModelAttribute PagerInfo pagerInfo, @ModelAttribute SysFeedback sysVipType) {
        pagerInfo.setCurrent(pagerInfo.getPageNumber());
        List<EntityMap> list = bizService.getAll(pagerInfo);
        PqGrid pqGrid = new PqGrid();
        pqGrid.setRows(list);
        pqGrid.setPageCount(list.size());
        pqGrid.setTotal(new Long(pagerInfo.getTotal()).intValue());
        pqGrid.setPages(pagerInfo.getPages());
        pqGrid.setPage(pagerInfo.getCurrent());
        return ResultBody.success(pqGrid);
    }

    @ApiOperation(value = "添加", notes = "添加")
    @Override
    public ResultBody add(@ModelAttribute SysFeedbackDto feedback) {
        feedback.setType("2");
        feedback.setStatus(0);
        feedback.setIsRead(0);
        Boolean tag = bizService.save(feedback);
        UpdateWrapper c = new UpdateWrapper();
        bizService.update(c.set(true, "status", 1).eq(true, "id", feedback.getFeedId()));
        return ResultBody.success("添加成功").setData(tag);
    }

    @ApiOperation(value = "更新", notes = "更新")
    @Override
    public ResultBody update(@ModelAttribute SysFeedback feedback) {
        Boolean tag = bizService.saveOrUpdate(feedback);
        return ResultBody.success("更新成功").setData(tag);
    }

    @ApiOperation(value = "删除", notes = "删除")
    @Override
    public ResultBody delByIds(@RequestParam String[] ids) {
        Boolean tag = bizService.removeByIds(CollectionUtil.toList(ids));
        return ResultBody.success(tag);
    }

    /**
     * 删除单个留言
     */
    @ApiOperation(value = "删除", notes = "删除")
    public ResultBody delOne(@PathVariable Long id) {
        Boolean tag = bizService.removeById(id);
        return ResultBody.success(tag);
    }

    /**
     * 删除单个留言
     */
    @ApiOperation(value = "删除", notes = "删除")
    public ResultBody delAll(@PathVariable Long userId) {
        CriteriaQuery cq = new CriteriaQuery<SysFeedback>();
        boolean tag = bizService.remove(cq.eq("userId", userId));
        return ResultBody.success(tag);
    }

    @Override
    public ResultBody getById(@PathVariable Long id) {
        SysFeedback feedback = bizService.getById(id);
        return ResultBody.success(feedback);
    }

    /**
     * 留言反馈详情
     */
    @ApiOperation(value = "留言反馈详情", notes = "留言反馈详情")
    public ResultBody feedbackInfo(@PathVariable Long userId) {
        PagerInfo pagerInfo = new PagerInfo();
        pagerInfo.setPageSize(1000);
        CriteriaQuery cq = new CriteriaQuery<EntityMap>(pagerInfo);
        cq.select("f.*,u.userName");
        cq.createAlias("user", "u");
        cq.orderByAsc("f.createTime");
        cq.eq("f.userId", userId);
        PqGrid pqGrid = bizService.pageList(cq);
        List<EntityMap> list = (List<EntityMap>) pqGrid.getRows();
        return ResultBody.success(list);
    }
}
